package response

import (
	"fitness/go-admin/pkg/errcode"
	"net/http"

	"github.com/gin-gonic/gin"
)

type Response struct {
	Code    int         `json:"code"`
	Message string      `json:"message"`
	Data    interface{} `json:"data,omitempty"`
}

type PageResponse struct {
	Code     int         `json:"code"`
	Message  string      `json:"message"`
	Data     interface{} `json:"data"`
	Total    int64       `json:"total"`
	Page     int         `json:"page"`
	PageSize int         `json:"pageSize"`
}

func Success(c *gin.Context, data interface{}, message string) {
	if message == "" {
		message = errcode.GetMessage(errcode.Success)
	}
	c.JSON(http.StatusOK, Response{
		Code:    errcode.Success,
		Message: message,
		Data:    data,
	})
}

func Error(c *gin.Context, code int, message string) {
	if message == "" {
		message = errcode.GetMessage(code)
	}
	c.JSON(http.StatusOK, Response{
		Code:    code,
		Message: message,
	})
}

// BadRequest 请求参数错误响应
func BadRequest(c *gin.Context, message string) {
	if message == "" {
		message = "请求参数错误"
	}
	c.JSON(http.StatusOK, Response{
		Code:    errcode.BadRequest,
		Message: message,
	})
}

// ErrorWithBusiness 业务错误响应
func ErrorWithBusiness(c *gin.Context, err *errcode.BusinessError) {
	c.JSON(http.StatusOK, Response{
		Code:    err.Code,
		Message: err.Message,
	})
}

func SuccessWithPage(c *gin.Context, data interface{}, total int64, page, pageSize int, message string) {
	if message == "" {
		message = errcode.GetMessage(errcode.Success)
	}
	c.JSON(http.StatusOK, PageResponse{
		Code:     errcode.Success,
		Message:  message,
		Data:     data,
		Total:    total,
		Page:     page,
		PageSize: pageSize,
	})
}
